package org.example;


import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.spring.AiService;
import org.example.assistant.Assistant;
import org.example.assistant.MemoryChatAssistant;
import org.example.assistant.SeparateChatAssistant;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;

@SpringBootTest
public class testChatMemory2 {


    @Autowired
    private QwenChatModel qwenChatModel;

    @Test
    public void testChatMemory2() {
//第一轮对话
        UserMessage userMessage1 = UserMessage.userMessage("我是环环");
        ChatResponse chatResponse1 = qwenChatModel.chat(userMessage1);
        AiMessage aiMessage1 = chatResponse1.aiMessage();
//输出大语言模型的回复
        System.out.println(aiMessage1.text());
//第二轮对话
        UserMessage userMessage2 = UserMessage.userMessage("你知道我是谁吗");
        ChatResponse chatResponse2 = qwenChatModel.chat(Arrays.asList(userMessage1,
                aiMessage1, userMessage2));
        AiMessage aiMessage2 = chatResponse2.aiMessage();
//输出大语言模型的回复
        System.out.println(aiMessage2.text());
    }


    @Test
    public void testChatMemory3() {
        MessageWindowChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);
        Assistant assistant = AiServices
                .builder(Assistant.class)
                .chatLanguageModel(qwenChatModel)
                .chatMemory(chatMemory)
                .build();
        String chat = assistant.chat("我是一娃");
        System.out.println(chat);

        String chat1 = assistant.chat("我是谁");
        System.out.println(chat1);


    }





    @Autowired
    private MemoryChatAssistant memoryChatAssistant;
    @Test
    public void testAssistant() {

        String chat = memoryChatAssistant.chat("我是一娃");
        System.out.println(chat);

        String chat1 = memoryChatAssistant.chat("我是谁");
        System.out.println(chat1);



    }


    @Autowired
    private SeparateChatAssistant separateChatAssistant;

    @Test
    public void testSeparateChat(){

        String chat = separateChatAssistant.chat(1, "我是一娃");
        System.out.println(chat);

        String chat1 = separateChatAssistant.chat(1, "我是谁");
        System.out.println(chat1);




        String chat2 = separateChatAssistant.chat(2, "我是谁");
        System.out.println(chat2);

    }





}
